﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetFrameworkExtensions.Utils
{
  public class DelegateReference<T> : ReferenceBase<T>
  {
    Func<T> getFunc;
    Action<T> setFunc;

    public DelegateReference(Func<T> getFunc, Action<T> setFunc = null)
    {
      if (getFunc == null) throw new ArgumentNullException("getFunc");
      this.getFunc = getFunc;
      this.setFunc = setFunc;
    }

    public override T Value
    {
      get
      {
        return getFunc();
      }
      set
      {
        CheckCanChange();
        setFunc(value);
        OnValueChanged(value);
      }
    }

    public override bool CanChange
    {
      get { return setFunc != null; }
    }
  }
}
